package top.qitun.bizz.account.tuser.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.base.service.BaseServiceImpl;
import com.google.common.base.Preconditions;
import com.google.common.html.HtmlEscapers;

import top.qitun.bizz.account.tuser.dao.TUserDao;
import top.qitun.bizz.account.tuser.service.TUserService;
import top.qitun.bizz.account.userrole.dao.UserRoleDao;
import top.qitun.bizz.user.role.entity.Role;
import top.qitun.bizz.user.tuser.entity.TUser;
import top.qitun.bizz.user.tuser.entity.TUserBO;
import top.qitun.bizz.user.userrole.entity.UserRole;
import top.qitun.bizz.user.userrole.entity.UserRoleBO;
import top.qitun.exception.RestStatusException;
import top.qitun.model.security.AuthenticationDTO;

/**
 * 
 * <br>
 * <b>功能：</b>TUserService<br>
 */
@Service
public class TUserServiceImpl extends BaseServiceImpl implements TUserService {
	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private TUserDao dao;

	@Autowired
	private UserRoleDao userRoleDao;


	public TUserDao getDao() {
		return dao;
	}

	public TUser selectByMobile(String escapeMobile) {
		return dao.selectByMobile(escapeMobile);
	}

	@Transactional(rollbackFor = Exception.class)
	public TUserBO register(TUserBO dbUser) throws Exception {
		Date now = new Date();
		dbUser.setBalance(0L);
		dbUser.setCreateTime(now);
		dbUser.setUpdateTime(now);
//		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//		final String loginPassword = passwordEncoder.encode(dbUser.getLoginPwd());
//		dbUser.setLoginPwd(loginPassword);
		// 混合盐后入库
		dao.add(dbUser);
		UserRoleBO userRole = new UserRoleBO();
		userRole.setRoleId(1);
		userRole.setUserId(dbUser.getId());
		userRoleDao.add(userRole);
		List<UserRoleBO> roleList = new ArrayList<UserRoleBO>();
		roleList.add(userRoleDao.queryById(1));
		dbUser.setRoleList(roleList);
		return dbUser;
	}

	public TUserBO login(AuthenticationDTO request) {
		Preconditions.checkNotNull(request);
		final TUserBO user = find(request.getMobile());
		if (user == null) {
			logger.info("用户不存在.", request);
			throw new RestStatusException(1, "用户不存在");
			// throw new
			// RestStatusException(UserStatusCode.USER_NOT_EXISTS.code(),"用户不存在.");
			// Shift.fatal(StatusCode.USER_NOT_EXISTS);
		}
		// 登录用户的密码摘要
		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		// final String requestLoginPWd = digestWithSalt(request.getLoginPwd(),
		// user.getPwdSalt());
		// if (!Objects.equal(requestLoginPWd, user.getLoginPwd())) {
		if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
			logger.info("用户名或者密码错误!", request);
			throw new RestStatusException(2, "用户名或者密码错误!");
			// throw new
			// RestStatusException(UserStatusCode.INVALID_CREDENTIAL.code(),"用户名或密码错误");
			// Shift.fatal(StatusCode.INVALID_CREDENTIAL);
		}
		return user;
	}

	public TUserBO find(String mobile) {
		Preconditions.checkNotNull(mobile);
		TUserBO result = null;
		if (!mobile.isEmpty()) {
			final String escapeMobile = HtmlEscapers.htmlEscaper().escape(mobile);
			result = dao.selectByMobile(escapeMobile);
		}
		return result;
	}

	@Override
	public TUserBO queryByMobile(String mobile) {
		return dao.queryByMobile(mobile);
	}
}
